package com.service;


import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

import java.io.IOException;

@RabbitListener(queues = {"topicQueue1"})
@Component
public class MQListener1 {



    @RabbitHandler
    public void recv(Message message, Channel channel)
    {
       // channel.basicReject(); //重发
      //  channel.basicNack(); //拒绝接收
      //  channel.basicAck(); //正确收到了
        try {
            System.out.println("队列topicQueue--1收到消息:"+new String(message.getBody(),"utf-8"));

            //调用业务层
            //没有错误就发送正确接收,消息编号，是否批量,true则比当前编号小的全部应答正确
            channel.basicAck(message.getMessageProperties().getDeliveryTag(),false);
        }catch (Exception e)
        {

            try {
                //拒绝，消息编号，是否批量，是否重新放回队列
                //如果放回队列，会放在队首，可以死循环
                channel.basicNack(message.getMessageProperties().getDeliveryTag(),false,false);
            } catch (IOException e1) {
                e1.printStackTrace();
            }
        }

            }
}
